package com.system.server;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import javax.imageio.spi.ImageTranscoderSpi;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class FoodInter {

    VBox finalBox=new VBox();
    private final ObservableList<Food> FoodData= FXCollections.observableArrayList();

    public VBox creatFoodUIPane(Object[] PaneList, Stage primaryStage)
    {
        GridPane pane=new GridPane();
        pane.setAlignment(Pos.BASELINE_CENTER);
        pane.setPadding(new Insets(50,50,50,50));
        pane.setHgap(10);
        pane.setVgap(10);

        Label title=new Label("商品管理");
        title.setFont(Font.font("商品管理", FontWeight.BOLD,20));
        Label tips=new Label("tips：请输入商品名查询某商品信息或者查询所有商品");
        Label food=new Label("商品名: ");
        TextField foodName=new TextField();
        Button selectBtn=new Button("查询商品");
        Button addBtn=new Button("添加商品");
        Button cancelBtn=new Button("返回");


        HBox hBox=new HBox();
        hBox.setSpacing(10);
        hBox.getChildren().add(food);
        hBox.getChildren().add(foodName);
        hBox.getChildren().add(selectBtn);
        hBox.getChildren().add(addBtn);



        pane.add(title,0,1);
        pane.add(tips,0,2);
        pane.add(hBox,0,3);

        Label tableName=new Label("商品信息");
        TableView<Food> table=new TableView<>();
        TableColumn<Food,Integer> IdCol=new TableColumn<>("ID");
        IdCol.setMaxWidth(25);
        IdCol.setCellValueFactory(new PropertyValueFactory<>("food_id"));

        TableColumn<Food,String> nameCol=new TableColumn<>("商品名");
        nameCol.setMinWidth(50);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("food_name"));

        TableColumn<Food,String> kindCol=new TableColumn<>("商品种类");
        kindCol.setMinWidth(50);
        kindCol.setCellValueFactory(new PropertyValueFactory<>("food_kind"));

        TableColumn<Food,String> valueCol=new TableColumn<>("商品价格");
        valueCol.setMinWidth(50);
        valueCol.setCellValueFactory(new PropertyValueFactory<>("food_value"));

        TableColumn<Food,String> numberCol=new TableColumn<>("库存");
        numberCol.setMinWidth(50);
        numberCol.setCellValueFactory(new PropertyValueFactory<>("food_number"));

        TableColumn<Food,String> saleCol=new TableColumn<>("销量");
        saleCol.setMinWidth(50);
        saleCol.setCellValueFactory(new PropertyValueFactory<>("food_sale"));

        table.setItems(FoodData);
        table.getColumns().addAll(IdCol,nameCol,kindCol,valueCol,numberCol,saleCol);

        VBox vBox=new VBox();
        vBox.getChildren().add(tableName);
        vBox.getChildren().add(table);

        pane.add(vBox,0, 4);
        pane.add(cancelBtn,0,5);

        selectBtn.setOnAction(actionEvent -> {
            String name=foodName.getText();
            if(!Objects.equals(name, "")){
                ResultSet res=FoodDAO.selectWithFoodName(name);
                try{
                    while(res.next()){
                        FoodData.clear();
                        Food food1=new Food();
                        food1.setFood_id(res.getInt("food_id"));
                        food1.setFood_name(res.getString("food_name"));
                        food1.setFood_kind(res.getString("food_kind"));
                        food1.setFood_value(res.getString("food_value"));
                        food1.setFood_number(res.getString("food_number"));
                        food1.setFood_sale(res.getString("food_sale"));
                        FoodData.add(food1);
                        foodName.clear();
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else{
                ResultSet res=FoodDAO.selectAllFood();
                FoodData.clear();
                try{
                    while(res.next()){
                        Food food1=new Food();
                        food1.setFood_id(res.getInt("food_id"));
                        food1.setFood_name(res.getString("food_name"));
                        food1.setFood_kind(res.getString("food_kind"));
                        food1.setFood_value(res.getString("food_value"));
                        food1.setFood_number(res.getString("food_number"));
                        food1.setFood_sale(res.getString("food_sale"));
                        FoodData.add(food1);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        addBtn.setOnAction(actionEvent -> {
            FoodUI ui=new FoodUI();
            ui.start(new Stage());
        });


        cancelBtn.setOnAction(actionEvent -> {
            finalBox.setVisible(false);
            ((VBox)PaneList[0]).setVisible(true);

            primaryStage.setWidth(300);
            primaryStage.setHeight(300);
            primaryStage.setTitle("管理员主界面");
        });

        finalBox.setAlignment(Pos.CENTER);
        finalBox.getChildren().addAll(pane);
        finalBox.setVisible(false);

        return finalBox;
    }

}
